﻿﻿//======================================================================
//
//        Copyright : Zhengzhou Strawberry Computer Technology Co.,LTD.
//        All rights reserved
//        
//        Application:NFinal MVC framework
//        Filename :CookieManager.cs
//        Description :Cookie管理类
//
//        created by Lucas at  2015-6-30`
//     
//        WebSite:http://www.nfinal.com
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Web;
namespace WebApplication1.DDD.Code.Data
{
    public class CookieManager
    {
#if (!AspNET && !MicrosoftOwin && !NFinalOwin)
        private HttpContext _context;
        private Microsoft.Owin.IOwinContext _webContext;
        private NFinal.Owin.HtmlWriter writer;
		private IDictionary<string, string> cookies;
#endif
#if AspNET
        private HttpContext _context;
#endif
#if MicrosoftOwin
        private Microsoft.Owin.IOwinContext _webContext;
#endif
#if NFinalOwin
        private NFinal.Owin.HtmlWriter writer;
		private IDictionary<string, string> cookies;
#endif
        private CookieInfo _cookieInfo;
        private NFinal.ServerType _serverType = NFinal.ServerType.UnKnown;
        public CookieManager()
        {
            this._serverType = NFinal.ServerType.IsStatic;
            _cookieInfo = new CookieInfo();
        }
#if (!AspNET && !MicrosoftOwin && !NFinalOwin)
        public CookieManager(HttpContext context)
        {
            this._serverType = NFinal.ServerType.AspNET;
            this._context = context;
            _cookieInfo = new CookieInfo();
        }
        public CookieManager(Microsoft.Owin.IOwinContext context)
        {
            this._webContext = context;
            this._serverType = NFinal.ServerType.MicrosoftOwin;
            _cookieInfo = new CookieInfo();
        }
        public CookieManager(NFinal.Owin.HtmlWriter writer, IDictionary<string, string> cookies)
        {
            this._serverType = NFinal.ServerType.NFinalOwin;
            this.cookies = cookies;
            this.writer = writer;
            _cookieInfo = new CookieInfo();
        }
#endif
#if AspNET
        public CookieManager(HttpContext context)
        {
            this._serverType = NFinal.ServerType.AspNET;
            this._context = context;
			_cookieInfo = new CookieInfo();
        }
#endif
#if MicrosoftOwin
        public CookieManager(Microsoft.Owin.IOwinContext context)
        {
            this._webContext = context;
            this._serverType = NFinal.ServerType.MicrosoftOwin;
            _cookieInfo = new CookieInfo();
        }
#endif
#if NFinalOwin
        public CookieManager(NFinal.Owin.HtmlWriter writer,IDictionary<string,string> cookies)
        {
            this._serverType = NFinal.ServerType.NFinalOwin;
            this.cookies = cookies;
            this.writer = writer;
            _cookieInfo = new CookieInfo();
        }
#endif
		public string this[string key]
        {
            get
            {
				string val=null;
#if (!AspNET && !MicrosoftOwin && !NFinalOwin)
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Request.Cookies["DDD_"+key] != null)
                    {
                        val = _context.Request.Cookies["DDD_"+key].Value;
                    }
                    else
                    {
                        val = Guid.NewGuid().ToString("N");
                        HttpCookie cookie = new HttpCookie("DDD_"+key, val);
                        _context.Response.Cookies.Add(cookie);
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["DDD_"+key] != null)
                    {
                        val = _webContext.Request.Cookies["DDD_"+key];
                    }
                    else
                    {
                        val = Guid.NewGuid().ToString("N");
                        _webContext.Response.Cookies.Append("DDD_"+key, val);
                    }
                }
				else if (this._serverType == NFinal.ServerType.NFinalOwin)
                {
                    if (cookies.ContainsKey("DDD_"+key))
                    {
                        return cookies["DDD_"+key];
                    }
                    else
                    {
                        return null;
                    }
                }
#endif
#if AspNET
				if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Request.Cookies["DDD_"+key] != null)
                    {
                        val = _context.Request.Cookies["DDD_"+key].Value;
                    }
                    else
                    {
                        val = Guid.NewGuid().ToString("N");
                        HttpCookie cookie = new HttpCookie("DDD_"+key, val);
                        _context.Response.Cookies.Add(cookie);
                    }
                }
#endif
#if MicrosoftOwin
                if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["DDD_"+key] != null)
                    {
                        val = _webContext.Request.Cookies["DDD_"+key];
                    }
                    else
                    {
                        val = Guid.NewGuid().ToString("N");
                        _webContext.Response.Cookies.Append("DDD_"+key, val);
                    }
                }
#endif
#if NFinalOwin
                if (this._serverType == NFinal.ServerType.NFinalOwin)
                {
                    if (cookies.ContainsKey("DDD_"+key))
                    {
                        return cookies["DDD_"+key];
                    }
                    else
                    {
                        return null;
                    }
                }
#endif
				return val;
            }
            set
            {
#if (!AspNET && !MicrosoftOwin && !NFinalOwin)
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Response.Cookies["session_id"] == null)
                    {
                        HttpCookie cookie = new HttpCookie("DDD_"+key, value);
                        _context.Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        _context.Request.Cookies["DDD_"+key].Value = value;
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["DDD_"+key] != null)
                    {
                        _webContext.Response.Cookies.Delete("DDD_"+key);
                    }
                    _webContext.Response.Cookies.Append("DDD_"+key, value);
                }
				else if (this._serverType == NFinal.ServerType.NFinalOwin)
                {
                    if (value == null)
                    {
                        writer.SetExpiredCookie("DDD_"+key);
                    }
                    else
                    {
                        writer.SetCookie("DDD_"+key, value);
                    }
                }
#endif
#if AspNET
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Response.Cookies["session_id"] == null)
                    {
                        HttpCookie cookie = new HttpCookie("DDD_"+key, value);
                        _context.Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        _context.Request.Cookies["DDD_"+key].Value = value;
                    }
                }
#endif
#if MicrosoftOwin
                if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["DDD_"+key] != null)
                    {
                        _webContext.Response.Cookies.Delete("DDD_"+key);
                    }
                    _webContext.Response.Cookies.Append("DDD_"+key, value);
                }
#endif
#if NFinalOwin
                if (this._serverType == NFinal.ServerType.NFinalOwin)
                {
                    if (value == null)
                    {
                        writer.SetExpiredCookie("DDD_"+key);
                    }
                    else
                    {
                        writer.SetCookie("DDD_"+key, value);
                    }
                }
#endif
            }
        }
		public string session_id
        {
            get{_cookieInfo.session_id = this["session_id"];return _cookieInfo.session_id;}
			set{_cookieInfo.session_id = value; this["session_id"]=_cookieInfo.session_id;}
        }
        public string vcheck
        {
            get{_cookieInfo.vcheck = this["vcheck"];return _cookieInfo.vcheck;}
			set{_cookieInfo.vcheck = value; this["vcheck"]=_cookieInfo.vcheck;}
        }
        public string check
        {
            get{_cookieInfo.check = this["check"];return _cookieInfo.check;}
			set{_cookieInfo.check = value; this["check"]=_cookieInfo.check;}
        }
        public string cart
        {
            get
            {
					_cookieInfo.cart = this["cart"];
				return _cookieInfo.cart;
            }
            set
            {
                _cookieInfo.cart = value;
					this["DDD_cart"]= value;
            }
        }
    }
}